using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

using Kenly.DBFramework.Gateway;

namespace Kenly.DBFramework.Reflection
{
    /// <summary>
    /// 
    /// </summary>
    public static class KeyUtility
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static PropertyInfo GetPrimaryKeyProperty(Type type)
        {
            if (type == null)
            {
                return null;
            }

            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                ColumnAttribute columnAttribute = AttributeUtility.GetColumnAttribute(propertyInfo);
                if (columnAttribute != null && columnAttribute.IsPrimaryKey)
                {
                    return propertyInfo;
                }
            }

            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetPrimaryKeyName(Type type)
        {
            PropertyInfo propertyInfo = KeyUtility.GetPrimaryKeyProperty(type);
            if (propertyInfo == null)
            {
                return "";
            }
            return AttributeUtility.GetColumnName(propertyInfo);
        }

        //public static object GetPrimaryKeyValue(object entity)
        //{
        //    PropertyInfo propertyInfo = GetPrimaryKeyPropertyInfo(entity.GetType());
        //    if (propertyInfo == null)
        //    {
        //        return null;
        //    }
        //    object value= propertyInfo.GetValue(entity, null);
        //    return value;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void GetPrimaryKey(object entity, out string key, out object value)
        {
            key = "";
            value = null;
            if (entity == null)
            {
                return;
            }

            PropertyInfo propertyInfo = KeyUtility.GetPrimaryKeyProperty(entity.GetType());
            if (propertyInfo == null)
            {
                return;
            }

            key = AttributeUtility.GetColumnName(propertyInfo);
            value = propertyInfo.GetValue(entity, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetKeyValue(object entity, string key)
        {
            if (entity == null)
            {
                return null;
            }

            PropertyInfo[] propertyInfos = entity.GetType().GetProperties();
            if (propertyInfos == null || propertyInfos.Length <= 0)
            {
                return null;
            }

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                ColumnAttribute attribute = AttributeUtility.GetColumnAttribute(propertyInfo);
                if (attribute != null && (attribute.IsPrimaryKey || attribute.IsForeignKey))
                {
                    if (AttributeUtility.GetColumnName(propertyInfo) == key)
                    {
                        return propertyInfo.GetValue(entity, null);
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="referenceType"></param>
        /// <returns></returns>
        public static string GetForeignKey(Type type, Type referenceType)
        {
            if (type == null || referenceType == null)
            {
                return "";
            }

            ColumnAttribute columnAttribute = null;
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                columnAttribute = AttributeUtility.GetColumnAttribute(propertyInfo);
                if (columnAttribute != null && columnAttribute.IsForeignKey && 
                    columnAttribute.ReferenceType == referenceType)
                {
                    return columnAttribute.ColumnName;
                }
            }

            return "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="value"></param>
        public static void SetPrimaryKey(object entity, object value)
        {
            if (entity == null)
            {
                return;
            }

            PropertyInfo propertyInfo = KeyUtility.GetPrimaryKeyProperty(entity.GetType());
            if (propertyInfo == null)
            {
                return;
            }
            if (propertyInfo.CanWrite)
            {
                propertyInfo.SetValue(entity, value, null);
            }
        }
    }
}
